<template>
  <div class="login">
    <div class="login_table">
      <div class="login_div_img">
        <img src="../assets/logo.png" alt="图标" />
      </div>
      <div class="login_form">
        <!-- 注意：
        如果要用input框验证功能  就必须：、
          1）form中必须有:model="XXXA" 和 :rules="XXXXB"
            特别注意：XXXA必须和data中的数据的总名字一样
                     XXXXB必须和data中的规则总名字一样
          2）是每个item中 加prop=“XXXXX” -->
        <el-form ref="loginForm" :model="loginForm" :rules="loginFormRules">
          <el-form-item prop="username">
            <el-input
              placeholder="请输入姓名"
              prefix-icon="el-icon-user-solid"
              v-model="loginForm.username"
            ></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              v-model="loginForm.password"
              show-password
              placeholder="请输入密码"
              prefix-icon="el-icon-lock"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="success" @click="loginsumit">提交</el-button>
            <el-button @click="resetForm(loginForm)">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "Login",
  data() {
    return {
      loginForm: {
        username: "admin",
        password: "123456",
      },
      loginFormRules: {
        username: [
          { required: true, message: "请输入姓名", trigger: "blur" },
          { min: 1, max: 7, message: "长度在 1 到 7 个字符", trigger: "blur" },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            min: 3,
            max: 10,
            message: "长度在 3 到 10 个字符",
            trigger: "blur",
          },
        ],
      },
    };
  },
  methods: {
    // loginsumit() {
    //   this.$refs.loginForm.validate((boolean) => {
    //     if (boolean) {
    //       const result = this.$http.post("login", this.loginForm);
    //       console.log(result);
    //     } else return;
    //   });
    // },
    //因为上面正常写法 返回的是Promise  所以需要加入async和await转换一下
    loginsumit() {
      this.$refs.loginForm.validate(async (boolean) => {
        if (boolean) {
          const { data: res } = await this.$http.post("login", this.loginForm);
          console.log(res);
          //将返回的一大串数据中的 data解构赋值 命名为res   这样输出res  就只有返回的data数据了
          if (res.meta.status != 200) {
            this.$message({
              showClose: true,
              message: "登陆失败！！！！原因为：" + res.meta.msg,
              type: "error",
            });
          } else {
            this.$message({
              showClose: true,
              message: "登陆成功！！！！",
              type: "success",
            });
            window.sessionStorage.setItem("token", res.data.token);
            // console.log(window.sessionStorage.getItem("token"));
            // .replace 表示无痕迹的跳转另一个界面
            // .push  表示跳转后生成历史记录（即可前进后退）
            this.$router.replace("/home");
          }
        } else {
          this.$message({
            showClose: true,
            message: "输入的数值不符合规则，无法进行访问！",
            type: "error",
          });
        }
      });
    },
    resetForm() {
      // console.log(this);
      this.$refs.loginForm.resetFields();
    },
  },
};
</script>

<style scoped>
.login {
  height: 100%;
  background-color: rgb(38, 74, 107);
  display: flex;
  justify-content: center;
  align-items: center;
}
.login_table {
  background-color: aliceblue;
  width: 650px;
  height: 400px;
}
.login_div_img {
  padding: 8px;
  display: flex;
  position: absolute;
  left: 50%;
  top: 13%;
  transform: translate(-50%);
  border-radius: 50%;
  box-shadow: 0 0 10px #aaaa9a;
}
.login_div_img img {
  border-radius: 50%;
  background-color: #eceeeb;
}
.login_form {
  margin-top: 10px;
  padding-left: 40px;
  padding-right: 40px;
  position: relative;
  top: 50%;
  transform: translateY(-50%);
}
.login_form div:nth-child(2) {
  margin-top: 50px;
}
.login_form div:nth-child(3) {
  margin-top: 20px;
  position: absolute;
  left: 65%;
}
.login_form div:nth-child(3) button:nth-child(1) {
  margin-right: 30px;
}
</style>